﻿namespace RestaurantMenu.Library
{
    using System;

    public class ConsoleRestaurantUI : IRestaurantUI
    {
        private readonly ProductList restaurantStock;
        private readonly Customer customer;

        public ConsoleRestaurantUI(ProductList restaurantStock, Customer customer)
        {
            this.restaurantStock = restaurantStock;
            this.customer = customer;
        }

        public void Run()
        {
            PrintProducts();
            while (true)
            {
                Console.WriteLine("Please select the product id:");
                int productIndex = int.Parse(Console.ReadLine());
                if (productIndex == 100)
                {
                    PrintProducts();
                }
                else if (productIndex == 99)
                {
                    Console.WriteLine(customer.CalculateTotalPrice());
                    Console.WriteLine(customer.TotalNutritionFacts());
                }
                else if (productIndex == -1)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("{0} ordered!", restaurantStock.AllProducts[productIndex].Product.Name);
                    customer.Order.AddProduct(restaurantStock.AllProducts[productIndex].Product, 1);
                    restaurantStock.RemoveProduct(restaurantStock.AllProducts[productIndex].Product);
                }
            }
        }

        public void PrintProducts()
        {
            for (int index = 0; index < restaurantStock.AllProducts.Count; index++)
            {
                Console.WriteLine("[{0}] {1}", index, restaurantStock.AllProducts[index].Product);
            }
        }
    }
}
